/*
 * @name: 抽奖商品管理-批量添加商品
 * @Author: along
 * @Date: 2020-06-09
 */
<template>
    <div class="container zdLuckAdd">
        <!-- table -->
        <el-table
            :data="tableData"
            height="100%"
            style="width: 100%;"
            border
            ref="zdLuckAdd"
        >
            <el-table-column
                prop="goods_cover"
                label="商品缩略图"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
                :width="100"
            >
                <template slot-scope="scope">
                    <img
                        :src="scope.row.goods_cover"
                        alt="加载失败"
                        class="zd-goods-cover"
                        @click="fnShowPreview(scope.row.goods_cover)"
                    >
                </template>
            </el-table-column>
            <el-table-column
                prop="goods_title"
                label="原商品标题"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
                :width="110"
            />
            <el-table-column
                prop="goods_now_title"
                label="商品标题"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
                :width="150"
            >
                <template slot-scope="scope">
                    <el-input
                        placeholder="请输入标题"
                        v-model="scope.row.goods_now_title"
                        size="small"
                        maxlength="40"
                        clearable
                    />
                </template>
            </el-table-column>
            <el-table-column
                prop="goods_price"
                label="商品售价"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            />
            <el-table-column
                prop="luck_price"
                label="抽奖价格"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
                :width="150"
            >
                <template slot-scope="scope">
                    <el-input
                        placeholder="请输入价格"
                        v-model="scope.row.luck_price"
                        clearable
                        size="small"
                        type="number"
                        min="0"
                    />
                </template>
            </el-table-column>
            <el-table-column
                prop="goods_stock"
                label="商品库存"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            />
            <el-table-column
                prop="win_number"
                label="中奖名额"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
                :width="150"
            >
                <template slot-scope="scope">
                    <el-input
                        placeholder="请输入名额"
                        v-model="scope.row.win_number"
                        clearable
                        size="small"
                        type="number"
                        min="0"
                    />
                </template>
            </el-table-column>
            <el-table-column
                prop="win_time"
                label="开奖时间"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
                :width="210"
            >
                <template slot-scope="scope">
                    <el-date-picker
                        v-model="scope.row.win_time"
                        type="datetime"
                        placeholder="选择开奖时间"
                        size="small"
                        :picker-options="fnGetPickerOptions()"
                        style="width:190px"
                        @change="fnSetTime(scope.row)"
                    />
                </template>
            </el-table-column>
            <el-table-column
                prop="leavel"
                label="可抽奖等级"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
                :width="150"
            >
                <template slot-scope="scope">
                    <el-select
                        v-model="scope.row.leavel"
                        clearable
                        placeholder="请选择"
                        size="small"
                    >
                        <el-option
                            v-for="item in leavelData"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value"
                        />
                    </el-select>
                </template>
            </el-table-column>
            <el-table-column
                prop="sort"
                label="排序号"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
                :width="160"
            >
                <template slot-scope="scope">
                    <el-input
                        placeholder="请输入排序号"
                        v-model="scope.row.sort"
                        clearable
                        size="small"
                        type="number"
                        min="0"
                    />
                </template>
            </el-table-column>
            <el-table-column
                label="操作"
                align="center"
                fixed="right"
                :width="140"
            >
                <template slot-scope="scope">
                    <div class="zd-button">
                        <el-button
                            type="primary"
                            class="curse-add"
                            size="small"
                            @click="fnChange(scope.row, true)"
                            v-if="!scope.row.flag"
                        >
                            删除
                        </el-button>
                        <el-button
                            type="info"
                            class="curse-add"
                            size="small"
                            @click="fnChange(scope.row, false)"
                            v-if="scope.row.flag"
                        >
                            恢复
                        </el-button>
                        <el-button
                            type="primary"
                            class="curse-add"
                            size="small"
                            @click="fnPrevData(scope.row, scope.$index)"
                            v-if="scope.$index > 0"
                        >
                            同上
                        </el-button>
                    </div>
                </template>
            </el-table-column>
        </el-table>
        <!-- /table -->

        <!-- foot -->
        <div class="zd-foot">
            <el-button
                type="primary"
                size="small"
                style="width:100px"
                @click="fnCreateShop()"
            >
                确定
            </el-button>
            <el-button
                type="info"
                size="small"
                style="margin-left:30px;width:100px"
                @click="()=> {
                    this.$router.push({
                        name: 'zdLuckDraw'
                    })
                }"
            >
                返回列表
            </el-button>
        </div>

        <!-- 图片预览 -->
        <img-preview ref="preview" />
    </div>
</template>

<script>
import imgPreview from '@/components/common/img-preview';
import { back } from '@/mixin/back';
export default {
    mixins: [back],
    components: {
        imgPreview
    },
    data () {
        return {
            click_time: 0,
            tableData: [],
            authResult: {},
            leavelData: [{
                value: -1,
                label: '所有用户'
            },{
                value: 0,
                label: 'L0'
            },{
                value: 1,
                label: 'E1'
            },{
                value: 2,
                label: 'E2'
            },{
                value: 3,
                label: 'E3'
            },{
                value: 4,
                label: 'E4'
            },{
                value: 5,
                label: 'E5'
            }]
        };
    },
    filters: {},
    computed: {},
    watch: {},
    mounted () {
        this.$store.commit('SET_HEADER', [{ title: "APP后台"},{title: '商城'},{title: '批量添加商品'}]);
        this.authResult = this.$route.meta.authResult;
        this.init();
    },
    methods: {
        /**
         *数据初始化
         */
        init () {
            let query = this.$route.query;

            if(query.data) {
                let list = JSON.parse(query.data);

                list.forEach(element => {
                    element.flag = false;
                    element.goods_now_title = element.goods_title;
                    element.luck_price = '';
                    element.win_number = '';
                    element.win_time = '';
                    element.leavel = '';
                    element.sort = '';
                });

                setTimeout(()=>{
                    this.tableData = list;
                    console.log(this.tableData, 'tableData');
                },100);
            }
        },

        /**
         * 确定创建抽奖商品
         */
        fnCreateShop () {
            let flag = false;
            let timeFlag = false;
            let luckFlag = false;
            let now_time = new Date().getTime();


            this.tableData.forEach(option => {
                if(option.goods_now_title == '' || option.luck_price == '' || option.win_number == '' || option.win_time == '' || option.leavel === '' || option.sort == '') {
                    flag = true;
                }
                if(option.win_time <= now_time) {
                    timeFlag = true;
                }
                if(option.luck_price == '0') {
                    luckFlag = true;
                }
            });

            if(now_time - this.click_time > 1000) { //防抖处理 5s内不允许重复创建
                this.click_time = now_time;
                setTimeout(()=>{
                    if(!flag) {
                        let list = [];

                        if(luckFlag) {
                            this.$notify({
                                title: '提示',
                                message: '抽奖价格不可以为0',
                                type: 'warning',
                                duration: 3000
                            });
                            return;
                        }

                        if(timeFlag) {
                            this.$notify({
                                title: '提示',
                                message: '开奖时间不能小于当前时间',
                                type: 'warning',
                                duration: 3000
                            });
                            return;
                        }

                        this.tableData.forEach(option => {
                            if(!option.flag) {
                                list.push({
                                    goods_no: option.goods_no,
                                    lottery_title: option.goods_now_title,
                                    lottery_price: option.luck_price,
                                    lottery_num: option.win_number,
                                    lottery_time: option.win_time / 1000,
                                    lottery_level: option.leavel,
                                    lottery_sort: option.sort,
                                    lottery_show: 1
                                });
                            }
                        });

                        if(list.length == 0) {
                            this.$notify({
                                title: '提示',
                                message: '添加商品不可以为空',
                                type: 'warning',
                                duration: 3000
                            });
                            return;
                        }

                        this.$post('/student/shop_lottery%5Cadd_batch',{
                            batch_json: JSON.stringify(list)
                        },resp => {
                            if(resp.code == 1) {
                                this.$notify({
                                    title: '成功',
                                    message: '创建成功',
                                    type: 'success'
                                });
                                setTimeout(() => {
                                    this.$router.push({
                                        name: 'zdLuckDraw'
                                    });
                                }, 300);
                            } else {
                                this.$notify({
                                    title: '警告',
                                    message: resp.msg,
                                    type: 'warning'
                                });
                            }
                        });
                    } else {
                        this.$notify({
                            title: '提示',
                            message: '请填写未填项,输入框内容不可以为空',
                            type: 'warning',
                            duration: 3000
                        });
                    }
                },100);
            } else {
                this.$notify({
                    title: '警告',
                    message: '请勿重复创建',
                    type: 'warning'
                });
            }
        },

        /**
         * 删除/恢复
         */
        fnChange (row, type) {
            this.$set(row, 'flag', type);
        },

        /**
         * 同上
         */
        fnPrevData (row, index) {
            let prevDate = this.tableData[index - 1];

            for(let key in prevDate) {
                if(key !== 'flag' && key !== 'goods_cover' && key !== 'goods_title' && key !== 'goods_price' && key !== 'goods_stock' && key !== 'goods_no') {
                    row[key] = prevDate[key];
                }
            }
        },

        /**
         *时间处理
         */
        fnSetTime (row) {
            let temp = new Date(row.win_time).getTime();

            this.$set(row, 'win_time', temp);
        },

        /**
         * 查看图片
         * @param   {String}    images      //单图预览
         * @param   {Array}     images      //多图预览
         */
        fnShowPreview(images) {
            if(images && Object.prototype.toString.call(images) == '[object String]') {
                this.$refs.preview.show([
                    images
                ]);
            } else if (images && Object.prototype.toString.call(images) == '[object Array]') {
                this.$refs.preview.show(images);
            } else {
                this.$message({
                    type: 'warning',
                    message: '图片类型错误'
                });
            }
        },

        /**
         * 默认时间加载
         */
        fnGetPickerOptions () {

            return {
                shortcuts : [{
                    text: '昨天',
                    onClick(picker) {
                        const date = new Date();

                        date.setTime(date.getTime() - 3600 * 1000 * 24);
                        picker.$emit('pick', date);
                    }
                },{
                    text: '今天',
                    onClick(picker) {
                        picker.$emit('pick', new Date());
                    }
                },{
                    text: '明天',
                    onClick(picker) {
                        const date = new Date();

                        date.setTime(date.getTime() + 3600 * 1000 * 24);
                        picker.$emit('pick', date);
                    }
                },{
                    text: '后天',
                    onClick(picker) {
                        const date = new Date();

                        date.setTime(date.getTime() + 3600 * 1000 * 24 * 2);
                        picker.$emit('pick', date);
                    }
                }]
            };
        },

        /**
         * 过滤函数
         */
        emptyFormatter(row, column, val) {
            if (!val) return '--';
            return val;
        }
    }
};
</script>

<style lang="less" scoped>
    .container {
        background-color: #fff;
        padding: 10px 15px 5px 15px;
        display: flex;
        flex-direction: column;
        position: relative;
        width: 100%;
        .header-nav {
            position: relative;
            display: flex;
            flex-wrap: wrap;
            align-items: center;
            justify-content: space-between;
            .header-left, .header-right {
                display: flex;
                align-items: center;
                flex-wrap: wrap;
            }
            .header-new-box {
                display: flex;
                margin-right: 15px;
                align-items: center;
                color: #606266;
                margin-bottom: 10px;
                white-space: nowrap;
                &-title {
                    margin-right: 2px;
                    white-space: nowrap;
                }
            }
        }
        /* 表单 */
        .scope-price {
            width: 50px;
            height: 50px;
        }
        /* 字体高亮 */
        .cursor-font {
            color: rgb(64, 158, 255);
            cursor: pointer;
        }
        /* 分页 */
        .pagination-wrapper {
            padding-top: 10px;
            padding-bottom: 10px;
            .el-pagination {
                float: right;
                padding: 0;
                background-color: transparent;
            }
        }
    }
    /* 排序 */
    .sort-wrap {
        display: flex;
        align-items: center;
        cursor: pointer;
        .sort-spliter {
            width: 1px;
            height: 12px;
            background: rgba(235, 235, 235, 1);
            margin: 0 10px;
            }
            .btn-sort {
            font-size: 14px;
            line-height: 1;
            color: #ccc;
            &:hover {
                color: #20a0ff;
            }
        }
    }
    .el-button-left {
        margin-left: 40px;
    }
    .el-button-right {
        margin-right: 10px;
    }
    .zd-goods-cover {
        width: 40px;
        height: 40px;
    }
    .zd-button {
        display: flex;
        align-items: center;
    }
    .zd-foot {
        width: 100%;
        height: 32px;
        display: flex;
        justify-content: center;
        margin-top: 25px;
        margin-bottom: 30px;
    }
</style>
<style lang="less">
    .zdLuckAdd{
        .el-table__body-wrapper .el-table__body .el-table__row td {
            border-right: none;
        }
        .el-table__header-wrapper .el-table__header .has-gutter tr th {
            border-right: none;
        }
        .el-table .el-table_1_column_6  .cell {
            position: relative;
        }
        .el-range-editor--small .el-range-separator{
            padding: 0 1px !important;
        }
        // .el-table__body-wrapper {
        //     &::-webkit-scrollbar {
        //     }
        // }
    }
</style>
